// pages/billingConfirmation/billingConfirmation.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    collection: [],
    contract: [],
    detail: {},
    invoice_ext: [],
    company: {},
    address: {},
    account: {},
    service_fee: '',
    account_id: '',
    enterprise_invoice_draf_id: '',
    imgSrcArr: [],
    picSrcArr: [],
    isLoadAccount:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)

    if (options) {
      let data = JSON.parse(options.data);
      this.getData(data.enterprise_invoice_draf_id)
      this.setData({
        service_fee: data.service_fee,
        enterprise_invoice_draf_id: data.enterprise_invoice_draf_id
      })
    }
  },
  async getAccountData() {
    wx.$api.getAccountData().then(res => {
      let {
        code,
        data
      } = res.data;
      if (code == 200) {
        if (data.lists && data.lists.length > 0) {
          for (let val of data.lists) {
            if (val.is_default == 1) {
              this.setData({
                account_id: val.natural_account_id,
                account: val
              })
            }
          }
        }
      }
    }).catch(ret => {
      wx.$Toast(ret.msg || "服务繁忙")
    })
  },
  getData(enterprise_invoice_draf_id) {
    let obj = {
      enterprise_invoice_draf_id: enterprise_invoice_draf_id
    }
    wx.$api.invoicedRraf(obj).then(res => {
      if (res.data.code == 200) {
        let {
          data
        } = res.data;
        let imgSrcArr = [],
          picSrcArr = [];

        for (let item of data.contract) {
          picSrcArr.push(item.resource_href)
        }
        for (let list of data.collection) {
          imgSrcArr.push(list.resource_href)
        }
        this.setData({
          collection: data.collection,
          contract: data.contract,
          invoice_ext: data.invoice_ext,
          company: data.company,
          address: data.address,
          detail: data.detail,
          categorys: data.categorys,
          picSrcArr: picSrcArr,
          imgSrcArr: imgSrcArr
        })
        console.log(this.data)
      }
    }).catch(ret => {
      wx.$Toast(ret.msg || "服务繁忙")
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  previewImage(e) {
    let {
      index,
      type
    } = e.currentTarget.dataset, that = this, previewImg;
    type == 0 ? previewImg = that.data.imgSrcArr : previewImg = that.data.picSrcArr;
    wx.previewImage({
      urls: previewImg,
      current: previewImg[index],
      success: (res) => {
        console.log(res)
      },
      fail: (res) => {
        console.log(res)
      },
      complete: (res) => {
        console.log(res)
      },
    })
  },
  save(e) {
    console.log(e)
    let that = this;
    if (!that.data.account_id) {
      return wx.$Toast("请选择付款账户信息");
    }
    wx.showLoading({
      title: '',
      mask: true
    })
    // 测试环境订阅模板ID
    // let tmplIds = ['sYyg1nR7ANz75p3kAnaI6nsgqCLfC2pH4fao5c65X7s'];
    // 正式环境订阅模板ID
    let tmplIds=['c01RUxtO2FbnYqmfaygdN2LwHkwX4lbudUVGh15nQEA'];
    wx.requestSubscribeMessage({
      tmplIds: tmplIds,
      success: function (res) {
        console.log(res)
      },
      fail(res) {
        console.log(res)
      },
      complete(res) {
        setTimeout(() => {
          wx.$api.applyConfirm({
            enterprise_invoice_draf_id: that.data.enterprise_invoice_draf_id,
            account_id: that.data.account_id
          }).then(res => {
            console.log(res.data)
            if (res.data.code == 200) {
              wx.hideLoading();
              wx.$Toast('提交成功！')
              setTimeout(() => {
                wx.reLaunch({
                  url: '/pages/openRecord/openRecord',
                })
              }, 500)
            }
          }).catch(ret => {
            wx.hideLoading();
            wx.$Toast(ret.msg || "服务繁忙")
          })
        }, 1000)
      },
    })
  },
  goEdit() {
    wx.navigateTo({
      url: '/pages/accountInfo/accountInfo',
    })
  },
  changeAccountInfo(params) {
    console.log(params)
    if (params) {
      this.setData({
        account: params.account,
        account_id: params.account.natural_account_id
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    if (this.data.isLoadAccount)
      await this.getAccountData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
